<?php
/**
 * Description of Character
 *
 * @author Winz
 */
namespace WinZ\DataMinerBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="WinZ\DataMinerBundle\Repository\CharacterRepository")
 * @ORM\Table(name="characters",uniqueConstraints={
 *          @ORM\UniqueConstraint(name="idxUnique", columns={"name", "serverId"})
 *           })
 * @ORM\HasLifecycleCallbacks()
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *          "character" = "Character",
 *          "farmer" = "Farmer",
 *          "seller" = "Seller",
 *          })
 * 
 */
class Character {
     /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    
     /**
     * @ORM\Column(type="string",nullable=false)
     */
    private $name;
    
    /**
     * @ORM\ManyToOne(targetEntity="Server", inversedBy="characters")
     * @ORM\JoinColumn(name="serverId", referencedColumnName="id", nullable=false)
     */
    private $server;

    /**
     * @ORM\ManyToOne(targetEntity="Account", inversedBy="characters")
     * @ORM\JoinColumn(name="accountId", referencedColumnName="id", nullable=true)
     */
    private $account;
    
     /**
     * @ORM\Column(type="boolean")
     */
    private $isSeller;

     /**
     * @ORM\Column(type="integer")
     */
    private $gold;

    /**
     * @ORM\Column(type="datetime")
     */
    private $created;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updated;    
    
    /**
     * @ORM\Column(type="datetime",nullable=true)
     */
    private $lastGather = null;
    
    /**
     * @ORM\OneToMany(targetEntity="Session", mappedBy="character")
     */
    private $sessions;
    
    /**
     * @ORM\OneToMany(targetEntity="GoldDelta", mappedBy="character",cascade={"persist"})
     */
    private $goldDeltas;

    /**
     *
     * @ORM\OneToOne(targetEntity="Session")
     * @ORM\JoinColumn(name="lastSessionId", referencedColumnName="id", nullable=true)
     */    
    private $lastSession;
    
    public function __toString() { return $this->name; }
    
    public function __construct()
    {
        $this->setCreated(new \DateTime());
        $this->setUpdated(new \DateTime());
        $this->setIsSeller(false);
        $this->setGold(0);
    }

    /**
     * @ORM\preUpdate
     */
    public function setUpdatedValue()
    {
       $this->setUpdated(new \DateTime());
    }
    
    
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Character
     */
    public function setName($name)
    {
        $this->name = $name;
    
        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set gold
     *
     * @param integer $gold
     * @return Character
     */
    public function setGold($gold)
    {
        if($this->gold != $gold) {
            $d = new \WinZ\DataMinerBundle\Entity\GoldDelta();
            $d->setDelta($gold - $this->gold);
            $d->setCharacter($this);
            $this->getGoldDeltas()->add($d);
        }
        
        $this->gold = $gold;
    
        return $this;
    }

    /**
     * Get gold
     *
     * @return integer 
     */
    public function getGold()
    {
        return $this->gold;
    }

    /**
     * Set server
     *
     * @param \WinZ\DataMinerBundle\Entity\Server $server
     * @return Character
     */
    public function setServer(\WinZ\DataMinerBundle\Entity\Server $server = null)
    {
        $this->server = $server;
    
        return $this;
    }

    /**
     * Get server
     *
     * @return \WinZ\DataMinerBundle\Entity\Server 
     */
    public function getServer()
    {
        return $this->server;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Character
     */
    public function setCreated($created)
    {
        $this->created = $created;
    
        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Character
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;
    
        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }

    /**
     * Set isSeller
     *
     * @param boolean $isSeller
     * @return Character
     */
    public function setIsSeller($isSeller)
    {
        $this->isSeller = $isSeller;
    
        return $this;
    }

    /**
     * Get isSeller
     *
     * @return boolean 
     */
    public function getIsSeller()
    {
        return $this->isSeller;
    }
    
    /**
     * Get isOnline
     * 
     * @return integer 
     */
    public function isOnline()
    {
        $now = date_create();
//        $diff = date_diff($now,$this->updated);
        $diff = $now->getTimestamp() - $this->getUpdated()->getTimestamp();
                
        
        return ($diff < 60);
    }
    
    
    /**
     * Set lastGather
     *
     * @param \DateTime $lastGather
     * @return Character
     */
    public function setLastGather($lastGather)
    {
        $this->lastGather = $lastGather;
    
        return $this;
    }

    /**
     * Get lastGather
     *
     * @return \DateTime 
     */
    public function getLastGather()
    {
        return $this->lastGather;
    }

    /**
     * Get last seen in minutes
     * 
     * @return integer 
     */
    public function timeLastSeen()
    {
        $now = date_create();
        return $now->getTimestamp() - $this->getUpdated()->getTimestamp();
    }

    public function timeSinceLastGather()
    {
        $now = date_create();
        if (!$this->lastGather)
            return null;

        return $now->getTimestamp() - $this->getLastGather()->getTimestamp();
    }
    
    /**
     * Is the character stuck ?
     * 
     * @param int $stuckTime Time in second from the las gather the character will be considered stuck
     * @return bool
     */
    public function isStuck($stuckTime = 90)
    {
        if (!$this->lastGather)
            return false;

        return $this->timeSinceLastGather()>$stuckTime;
    }
    


    /**
     * Add sessions
     *
     * @param \WinZ\DataMinerBundle\Entity\Session $sessions
     * @return Character
     */
    public function addSession(\WinZ\DataMinerBundle\Entity\Session $sessions)
    {
        $this->sessions[] = $sessions;
    
        return $this;
    }

    /**
     * Remove sessions
     *
     * @param \WinZ\DataMinerBundle\Entity\Session $sessions
     */
    public function removeSession(\WinZ\DataMinerBundle\Entity\Session $sessions)
    {
        $this->sessions->removeElement($sessions);
    }

    /**
     * Get sessions
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSessions()
    {
        return $this->sessions;
    }

    /**
     * Set lastSession
     *
     * @param \WinZ\DataMinerBundle\Entity\Session $lastSession
     * @return Character
     */
    public function setLastSession(\WinZ\DataMinerBundle\Entity\Session $lastSession = null)
    {
        $this->lastSession = $lastSession;
    
        return $this;
    }

    /**
     * Get lastSession
     *
     * @return \WinZ\DataMinerBundle\Entity\Session 
     */
    public function getLastSession()
    {
        return $this->lastSession;
    }

    /**
     * Set account
     *
     * @param \WinZ\DataMinerBundle\Entity\Account $account
     * @return Character
     */
    public function setAccount(\WinZ\DataMinerBundle\Entity\Account $account = null)
    {
        $this->account = $account;
    
        return $this;
    }

    /**
     * Get account
     *
     * @return \WinZ\DataMinerBundle\Entity\Account 
     */
    public function getAccount()
    {
        return $this->account;
    }

    /**
     * Add goldDeltas
     *
     * @param \WinZ\DataMinerBundle\Entity\GoldDelta $goldDeltas
     * @return Character
     */
    public function addGoldDelta(\WinZ\DataMinerBundle\Entity\GoldDelta $goldDeltas)
    {
        $this->goldDeltas[] = $goldDeltas;
    
        return $this;
    }

    /**
     * Remove goldDeltas
     *
     * @param \WinZ\DataMinerBundle\Entity\GoldDelta $goldDeltas
     */
    public function removeGoldDelta(\WinZ\DataMinerBundle\Entity\GoldDelta $goldDeltas)
    {
        $this->goldDeltas->removeElement($goldDeltas);
    }

    /**
     * Get goldDeltas
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGoldDeltas()
    {
        return $this->goldDeltas;
    }
}